package de.cbf.cam.map2d.views;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IPartListener2;
import org.eclipse.ui.IPartService;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.part.ViewPart;

import de.cbf.cam.map2d.ActivatorMap2D;
import de.cbf.cam.map2d.map.Map;
import de.cbf.cam.map2d.shapes.Shape;

public class ViewMap2D extends ViewPart implements IPartListener2 {
	/**	 * The ID of the view as specified by the extension.	 */
	public static final String ID = "de.cbf.cam.map2d.views.ViewMap2D";
	
	
	private Map map;
//	private Composite compositePanelControl;
	
	private List<ActionListener> mouseDownListeners = new ArrayList<ActionListener>();
	private List<ActionListener> mouseDoubleClickListeners = new ArrayList<ActionListener>();
	private List<ActionListener> mouseUpListeners = new ArrayList<ActionListener>();
	
	/**
	 * The constructor.
	 */
	public ViewMap2D() {		
		IPartService service = ActivatorMap2D.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
		service.addPartListener(this); 
	}

	@Override
	public void createPartControl(Composite parent) {			
		map = new Map(parent,SWT.NONE);
		map.setLayoutData(new GridData(GridData.FILL_BOTH));
		map.registerMouseDoubleClickListener(new MouseDoubleClickListener());
		map.registerMouseDownListener(new MouseDownListener());
		map.registerMouseUpListener(new MouseUpListener());
	}

	@Override
	public void setFocus() {		
	}	
	
	public void addShape(Shape shape){
		map.addObjectToMap(shape);
	}
	
	public void removeShape(Shape shape,Point position){
		map.removeObjectFromMap(shape);
	}

	public Map getMap() {		
		return map;
	}
//	========================
//		listener registry
//	========================
	public boolean registerMouseDownListener(ActionListener listener){
		return mouseDownListeners.add(listener);
	}
	public boolean registerMouseDoubleClickListener(ActionListener listener){
		return mouseDoubleClickListeners.add(listener);
	}
	public boolean registerMouseUpListener(ActionListener listener){
		return mouseUpListeners.add(listener);
	}
	public boolean removeMouseDownListener(ActionListener listener){
		return mouseDownListeners.remove(listener);
	}	
	public boolean removeMouseDoubleClickListener(ActionListener listener){
		return mouseDoubleClickListeners.remove(listener);
	}	
	public boolean removeMouseUPListener(ActionListener listener){
		return mouseUpListeners.remove(listener);
	}	
//	========================
//		part control
//	========================
	@Override
	public void partActivated(IWorkbenchPartReference partRef) {
	}
	@Override
	public void partBroughtToTop(IWorkbenchPartReference partRef) {
	}
	@Override
	public void partClosed(IWorkbenchPartReference partRef) {
	}
	@Override
	public void partDeactivated(IWorkbenchPartReference partRef) {
	}
	@Override
	public void partHidden(IWorkbenchPartReference partRef) {
	}
	@Override
	public void partInputChanged(IWorkbenchPartReference partRef) {
	}
	@Override
	public void partOpened(IWorkbenchPartReference partRef) {
		if(partRef.getId().equals(ID)){
			ActivatorMap2D.getDefault().setView(this);
		}
	}
	@Override
	public void partVisible(IWorkbenchPartReference partRef) {
	}

	private class MouseDoubleClickListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			ActionEvent event = new ActionEvent(ViewMap2D.this, ActionEvent.ACTION_PERFORMED, e.getActionCommand());
			for (ActionListener listener : mouseDoubleClickListeners) {
				listener.actionPerformed(event);
			}			
		}		
	}
	private class MouseDownListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			ActionEvent event = new ActionEvent(ViewMap2D.this, ActionEvent.ACTION_PERFORMED, e.getActionCommand());
			for (ActionListener listener : mouseDownListeners) {
				listener.actionPerformed(event);
			}			
		}		
	}
	private class MouseUpListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			ActionEvent event = new ActionEvent(ViewMap2D.this, ActionEvent.ACTION_PERFORMED, e.getActionCommand());
			for (ActionListener listener : mouseUpListeners) {
				listener.actionPerformed(event);
			}			
		}		
	}

	
}